home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / E-Z Progress Bar 1.0a / ChiselClass.cp next >
Text File  |  1995-03-14  |  7KB  |  381 lines

  1. #include "ChiselClass.h"
  2.  
  3. void    ChiselClass::InitializeVars(short options)
  4. {
  5.     useRGB=RGBAvailable();
  6.     
  7.     darkGrey.red=darkGrey.green=darkGrey.blue=0xAAAA;
  8.     myDirtyWhite.red=myDirtyWhite.blue=myDirtyWhite.green=0xF000;
  9.     myWhite.red=myWhite.green=myWhite.blue=0xFFFF;
  10.     myBlack.red=myBlack.green=myBlack.blue=0x0000;
  11.     
  12.     borderWidth=3;
  13.     blackWidth=borderWidth/2;
  14.     
  15. //    kColorPress=0x0D00;            //    Good for thousands of colors
  16.     kColorPress=0x0440;            //    Acceptable for 256 colors
  17.     kMaxColor=0XEEEE;
  18.     kMinColor=0x0000;
  19.     
  20.     SetOptions(options);
  21. }
  22.  
  23. void    ChiselClass::SetOptions(short options)
  24. {
  25.     liteChisel=(options & dirtyWhite) ? myDirtyWhite : myWhite;
  26.     addBlackBorder=!(options & noBlackBorder);
  27.     chiselWay=!(options & chiselIn);
  28.     
  29.     darkChisel=darkGrey;
  30.     
  31. }
  32.  
  33. ChiselClass::ChiselClass(short options)
  34. {
  35.     InitializeVars(options);
  36.     
  37.     useDefaultRect=0;
  38. }
  39.  
  40. ChiselClass::ChiselClass(Rect defaultRect, short options)
  41. {
  42.     InitializeVars(options);
  43.     useDefaultRect=1;
  44.     dRect=defaultRect;
  45. }
  46.  
  47. void    ChiselClass::Chisel(Rect theRect, Boolean out)
  48. {
  49.     PenState    oldState;
  50.     RGBColor    oldColor;
  51.     
  52.     GetPenState(&oldState);
  53.     GetForeColor(&oldColor);
  54.     
  55.     PenSize(borderWidth, borderWidth);
  56.     PenMode(patCopy);
  57.  
  58.     if (addBlackBorder) {
  59.         PenSize(blackWidth, blackWidth);
  60.         
  61.         InsetRect(&theRect, -blackWidth, -blackWidth);
  62.         ForeColor(blackColor);
  63.         FrameRect(&theRect);
  64.         
  65.         if (!out)
  66.             MoveTo(theRect.left+blackWidth/2, theRect.top);
  67.         
  68.         else 
  69.             MoveTo(theRect.right-blackWidth-blackWidth/2, theRect.top);
  70.         
  71.         if ((blackWidth/2)<1)
  72.             Move((out) ? -1 : 1, 0);
  73.         
  74.         Line(0, theRect.bottom-theRect.top-blackWidth);            //    Add thick border on side
  75.     
  76.         MoveTo(theRect.left, theRect.bottom-blackWidth-blackWidth/2);
  77.  
  78.         if ((blackWidth/2)<1)
  79.             Move(0, -1);
  80.             
  81.         Line(theRect.right-theRect.left, 0);                        //    Add thick border on bottom
  82.  
  83.         PenSize(borderWidth, borderWidth);
  84.     }
  85.     
  86.     InsetRect(&theRect, -borderWidth, -borderWidth);
  87.     
  88.     if (!out) {
  89.         if (useRGB)
  90.             RGBForeColor(&darkChisel);
  91.         
  92.         else
  93.             PenPat(<Gray);
  94.     }
  95.     
  96.     else {
  97.         if (useRGB)
  98.             RGBForeColor(&liteChisel);
  99.         else
  100.             PenPat(&white);
  101.     }
  102.         
  103.     MoveTo(theRect.left, theRect.bottom-borderWidth);
  104.     LineTo(theRect.left, theRect.top);
  105.     LineTo(theRect.right-borderWidth, theRect.top);
  106.     
  107.     if (!out) {
  108.         if (useRGB)
  109.             RGBForeColor(&liteChisel);
  110.         else
  111.             PenPat(&white);
  112.     }
  113.     
  114.     else {
  115.         if (useRGB)
  116.             RGBForeColor(&darkChisel);
  117.         
  118.         else
  119.             PenPat(<Gray);
  120.     }
  121.     
  122.     MoveTo(theRect.right-borderWidth, theRect.top+borderWidth);
  123.     LineTo(theRect.right-borderWidth, theRect.bottom-borderWidth);
  124.     LineTo(theRect.left+borderWidth, theRect.bottom-borderWidth);
  125.     
  126.     if (!useRGB) {
  127.         InsetRect(&theRect, -1, -1);
  128.         PenSize(1, 1);
  129.         PenPat(&black);
  130.         FrameRect(&theRect);
  131.     }
  132.     
  133.     SetPenState(&oldState);
  134.     RGBForeColor(&oldColor);
  135. }
  136.  
  137. void    ChiselClass::ChiselIn(Rect theRect)
  138. {
  139.     Chisel(theRect, 0);
  140. }
  141. void    ChiselClass::ChiselOut(Rect theRect)
  142. {
  143.     Chisel(theRect, 1);
  144. }
  145.  
  146. void    ChiselClass::Chisel()
  147. {
  148.     if (useDefaultRect)
  149.         Chisel(dRect, chiselWay);
  150. }
  151.  
  152. void    ChiselClass::Chisel(Rect theRect)
  153. {
  154.     Chisel(theRect, chiselWay);
  155. }
  156.  
  157. void    ChiselClass::HairChisel()
  158. {
  159.     if (useDefaultRect)
  160.         HairChisel(dRect, chiselWay);
  161. }
  162.  
  163. void    ChiselClass::HairChisel(Rect theRect)
  164. {
  165.     HairChisel(theRect, chiselWay);
  166. }
  167.  
  168. void    ChiselClass::HairChiselOut(Rect theRect)
  169. {
  170.     HairChisel(theRect, 1);
  171. }
  172.  
  173. void    ChiselClass::HairChiselIn(Rect theRect)
  174. {
  175.     HairChisel(theRect, 0);
  176. }
  177.  
  178. void    ChiselClass::HairChisel(Rect &theRect, Boolean out)
  179. {
  180.     RGBColor    oldColor;
  181.     PenState    oldState;
  182.     short        width=theRect.right-theRect.left, height=theRect.bottom-theRect.top;
  183.     
  184.     GetPenState(&oldState);
  185.     
  186.     PenSize(1,1);
  187.     PenMode(patCopy);
  188.  
  189.     if (useRGB) {
  190.         GetForeColor(&oldColor);
  191.         
  192.         if (out)
  193.             RGBForeColor(&liteChisel);
  194.         else
  195.             RGBForeColor(&darkChisel);
  196.             
  197.         MoveTo(theRect.left, theRect.bottom);
  198.         Line(0, -height);
  199.         Line(width, 0);
  200.         
  201.         if (out)
  202.             RGBForeColor(&darkChisel);
  203.         else
  204.             RGBForeColor(&liteChisel);
  205.             
  206.         Line(0, height);
  207.         Line(-width, 0);
  208.         
  209.         RGBForeColor(&oldColor);
  210.     }
  211.     
  212.     else {
  213.         ForeColor(whiteColor);
  214.         MoveTo(theRect.right, theRect.top);
  215.         Line(0, height);
  216.         Line(-width, 0);
  217.  
  218.         ForeColor(blackColor);
  219.         Line(0, -height);
  220.         Line(width, 0);
  221.     }
  222.     
  223.     SetPenState(&oldState);
  224. }
  225.  
  226. Rect    ChiselClass::ChiselRect(Rect theRect)
  227. {
  228.     InsetRect(&theRect, -borderWidth, -borderWidth);
  229.     if (addBlackBorder)
  230.         InsetRect(&theRect, -blackWidth, -blackWidth);
  231.     if (!useRGB)
  232.         InsetRect(&theRect, -borderWidth, -borderWidth);
  233.     return theRect;
  234. }
  235.  
  236. Rect    ChiselClass::ChiselRect()
  237. {
  238.     if (useDefaultRect)
  239.         return ChiselRect(dRect);
  240.     return dRect;
  241. }
  242.  
  243. Rect    ChiselClass::PreChisel(Rect theRect)
  244. {
  245.     InsetRect(&theRect, borderWidth, borderWidth);
  246.     if (addBlackBorder)
  247.         InsetRect(&theRect, blackWidth, blackWidth);
  248.     if (!useRGB)
  249.         InsetRect(&theRect, borderWidth, borderWidth);
  250.     return theRect;
  251. }
  252.  
  253. Rect    ChiselClass::PreChisel()
  254. {
  255.     if (useDefaultRect)
  256.         return PreChisel(dRect);
  257.     return dRect;
  258. }
  259.  
  260. void    ChiselClass::CleanRect(Rect theRect)
  261. {
  262.     theRect=ChiselRect(theRect);
  263.     EraseRect(&theRect);
  264. }
  265.  
  266. void    ChiselClass::CleanRect()
  267. {
  268.     if (useDefaultRect)
  269.         CleanRect(dRect);
  270. }
  271.  
  272. void    ChiselClass::InvalChisel(Rect theRect)
  273. {
  274.     theRect=ChiselRect(theRect);
  275.     InvalRect(&theRect);
  276. }
  277.  
  278. void    ChiselClass::InvalChisel()
  279. {
  280.     if (useDefaultRect)
  281.         InvalChisel(dRect);
  282. }
  283.  
  284. void    ChiselClass::SetDefaultRect(Rect &theRect)
  285. {
  286.     dRect=theRect;
  287.     useDefaultRect=1;
  288. }
  289.  
  290. void    ChiselClass::SetDarkChiselColor(RGBColor darkColor)
  291. {
  292.     darkChisel=darkColor;
  293. }
  294.  
  295. void    ChiselClass::SetLiteChiselColor(RGBColor liteColor)
  296. {
  297.     liteChisel=liteColor;
  298. }
  299.  
  300. void    ChiselClass::LiteDarkFrom(RGBColor baseColor)
  301. {
  302.     LiteFrom(baseColor);
  303.     DarkFrom(baseColor);
  304. }
  305.  
  306. void    ChiselClass::LiteFrom(RGBColor baseColor)
  307. {
  308.     int    hi=GetHi(baseColor);
  309.     
  310.     int    amount=((hi+kColorPress)>kMaxColor) ? kMaxColor-hi : kColorPress;
  311.     
  312.     baseColor.red+=amount;
  313.     baseColor.green+=amount;
  314.     baseColor.blue+=amount;
  315.     SetLiteChiselColor(baseColor);
  316.     
  317. }
  318.  
  319. void    ChiselClass::DarkFrom(RGBColor baseColor)
  320. {
  321.     int    lo=GetLo(baseColor);
  322.     
  323.     int    amount=((lo-kColorPress)<kMinColor) ? lo-kMinColor : kColorPress;
  324.     
  325.     baseColor.red-=amount;
  326.     baseColor.green-=amount;
  327.     baseColor.blue-=amount;
  328.     SetDarkChiselColor(baseColor);
  329.     
  330. }
  331.  
  332. int    ChiselClass::GetHi(RGBColor &color)
  333. {
  334.     int    max=color.red;
  335.     
  336.     max=(color.green>max) ? color.green : max;
  337.     max=(color.blue>max) ? color.blue : max;
  338.     
  339.     return max;
  340. }
  341.  
  342. int    ChiselClass::GetLo(RGBColor &color)
  343. {
  344.     int    min=color.red;
  345.     
  346.     min=(color.green<min) ? color.green : min;
  347.     min=(color.blue<min) ? color.blue : min;
  348.     
  349.     return min;
  350. }
  351.  
  352. void    ChiselClass::SetFrameColor(RGBColor frameColor)
  353. {
  354.     myBlack=frameColor;
  355. }
  356.  
  357. void    ChiselClass::SetChiselWidth(short width)
  358. {
  359.     borderWidth=width;
  360.     blackWidth=borderWidth/2;
  361. }
  362.  
  363. void    ChiselClass::SetBlackBorderWidth(short width)
  364. {
  365.     blackWidth=width;
  366. }
  367.  
  368. void    ChiselClass::SetChiselWay(short chiselDir)
  369. {
  370.     chiselWay=!(chiselIn & chiselDir);
  371. }
  372.  
  373. void    ChiselClass::UseDirtyWhite(Boolean dirty)
  374. {
  375.     liteChisel=(dirty) ? myDirtyWhite : myWhite;
  376. }
  377.  
  378. void    ChiselClass::UseBlackFrame(Boolean useBlackBorder)
  379. {
  380.     addBlackBorder=useBlackBorder;
  381. }